//
//  ZSLOAuthViewController.swift
//  micro_blog
//
//  Created by Mac on 16/9/21.
//  Copyright © 2016年 Mac. All rights reserved.
//

import UIKit

//  新浪开发平台提供的APPKey
let WeiboAppKey = "2707543009"
//  授权回调页
let WeiboRedirect_Uri = "http://www.itcast.cn"


class ZSLOAuthViewController: UIViewController {
    
    private lazy var webView: UIWebView = UIWebView()
    
    override func loadView() {
        
        webView.isOpaque = false
        
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
 
        requestLogin()
       
        setupNavUI()
    }

    //第三方登录
    private func requestLogin() {
        
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(WeiboAppKey)&redirect_uri=\(WeiboRedirect_Uri)"
        
        let request = URLRequest(url: URL(string: url)!)
        
        webView.loadRequest(request)
        
        
        
    }
    
    private func setupNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(cancelAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(autoFillAction))
        title = "微博"
        
        
    }
    
    @objc private func cancelAction() {
        
        dismiss(animated: true, completion: nil)
        
    }

    @objc private func autoFillAction() {
        
        print("自动填充")
        
        
        webView.stringByEvaluatingJavaScript(from: "document.getElementById('userId').value = 'hao123guohaibin@163.com';document.getElementById('passwd').value = 'guohaibin123'")
        
        
        
        
    }

}
